<template>
    <div>
        <a-modal
                centered
                :title="title"
                width="100%"
                :visible="visible"
                @ok="handleOk"
                @cancel="handleCancel"
                cancelText="取消绑定"
        >

            <!-- 查询区域 -->
            <div class="table-page-search-wrapper">
                <a-form layout="inline" @keyup.enter.native="searchQuery" >
                    <a-row :gutter="24">
                        <a-col :span="10">
                            <a-form-item label="机构名称">
                                <a-input placeholder="请输入机构名称" v-model="queryParam.customerName"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="10" v-if="showSelectArea">
                            <a-form-item label="区 域 查 询">
                                <select-area ref="selectAreaRef" @change="changeArea"></select-area>
                            </a-form-item>
                        </a-col>
                        <a-col :span="8">
                            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                            </span>
                        </a-col>
                    </a-row>
                </a-form>
            </div>

            <!-- table区域-begin -->
            <a-row :gutter="24">
                <a-col :span="selectCustomerList.length ? 12 : 24">
                    <h2>待选择列表</h2>
                    <a-table
                            size="small"
                            bordered
                            rowKey="id"
                            :columns="columns"
                            :dataSource="dataSource"
                            :pagination="ipagination"
                            :loading="loading"
                            :scroll="{ y: 800 }"
                            :rowSelection="{selectedRowKeys: selectedRowKeys,onChange: onSelectChange}"
                            @change="handleTableChange">
                    </a-table>
                </a-col>
                <a-col :span="12" v-if="selectCustomerList.length">
                    <div class="table-page-search-wrapper">
                        <a-form-model layout="inline" @keyup.enter.native="searchQuery" :rules="validatorRules" >
                            <a-row :gutter="24">
                                <a-col :span="10">
                                    <a-form-model-item label="路 线 选 择" prop="lines">
                                        <select-line-list :linesId = 'form.recyclePersonId' @change="changeLine" ></select-line-list>
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-form-model>
                    </div>
                    <h2>已选择列表</h2>
                    <a-table
                            size="small"
                            bordered
                            rowKey="id"
                            :columns="columns1"
                            :pagination="false"
                            :dataSource="selectCustomerList"
                            :scroll="{ y: 800 }">
                            <span slot="action" slot-scope="text, record">
                              <a @click="deleteData(record)">删除</a>
                            </span>
                    </a-table>
                </a-col>
            </a-row>
            <!-- table区域-end -->
        </a-modal>
    </div>
</template>

<script>
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import { postAction } from '@/api/manage'
    import SelectArea from '@/components/Jane/SelectArea'
    import SelectLineList from '@comp/Jane/SelectLineList'

    export default {
        mixins: [JeecgListMixin],
        components: {
            SelectLineList,
            SelectArea,
        },
        data() {
            return {
                title: '收运员绑定机构',
                names: [],
                visible: false,
                description: '',
                validatorRules: {
                    lines: [{ required: true, message: '请输入路线!', trigger: 'blur' }],
                },
                // 表头
                columns: [
                    {
                        title: '#',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 50,
                        align: 'center',
                        customRender: function(t, r, index) {
                            return parseInt(index) + 1
                        }
                    },
                    // {
                    //     title: '机构编号',
                    //     align: 'center',
                    //     dataIndex: 'customerNumber'
                    // },
                    {
                        title: '机构名称',
                        align: 'center',
                        dataIndex: 'customerName'
                    },
                    {
                        title: '负责人',
                        align: 'center',
                        dataIndex: 'leadername'
                    },
                    {
                        title: '负责人电话',
                        align: 'center',
                        dataIndex: 'leaderphone'
                    },
                    {
                        title: '机构地址',
                        align: 'center',
                        dataIndex: 'address',
                        customRender: function(t, r, index) {
                            return `${r.province || ''}${r.city || ''}${r.area || ''}`
                        }
                    }
                ],
                columns1: [
                    {
                        title: '序号',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 50,
                        align: 'center',
                        customRender: function(t, r, index) {
                            return parseInt(index) + 1
                        }
                    },
                    {
                        title: '机构名称',
                        align: 'center',
                        dataIndex: 'customerName'
                    },
                    {
                        title: '负责人',
                        align: 'center',
                        dataIndex: 'leadername'
                    },
                    {
                        title: '负责人电话',
                        align: 'center',
                        dataIndex: 'leaderphone'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        dataIndex: 'id',
                        scopedSlots: { customRender: 'action' }
                    }
                ],
                url: {
                    list: '/hc/hcRecycleCustomerInfo/list',
                    bind : '/hc/hcRecyclerCustomer/bindCustomer'
                },

                form : {
                    recyclePersonId : '',
                    lineId: '',
                },

                selectCustomerList : [],
            }
        },
        computed: {
            showSelectArea: function() {
                let state = true
                let userInfo = this.$store.getters.userInfo
                if (userInfo.provinceCode && userInfo.cityCode && userInfo.areaCode) {
                    state = false
                }
                return state
            }
        },
        methods: {
            async changeArea({ area, city }) {
                if (city) {
                    this.queryParam.cityCode = city
                }
                if (area) {
                    this.queryParam.areaCode = area
                }
            },
            async changeLine(value,info,option){
                this.form.lineId = value;
                this.searchQuery()
            },
            show(recycleUser) {
                this.visible = true
                // 根据当前用户去查询
                let userInfo = this.$store.getters.userInfo
                if (userInfo.companysId) {
                    this.queryParam.companysId = userInfo.companysId
                }
                // 收运员信息
                if (recycleUser) {
                    this.form.recyclePersonId = recycleUser.id;
                    this.form.recyclePersonName = recycleUser.realname || recycleUser.username || '--';
                }
                this.loadData(1)
            },
            searchQuery() {
                this.loadData(1)
            },
            handleOk() {
                let list = this.selectCustomerList;
                if(this.form.lineId == null || this.form.lineId == ''){
                    this.$message.info('请选择路线！');
                    return;
                }
                if(!list.length){
                    this.$message.info('请选择绑定机构');
                    return;
                }
                let params = {
                    customerNumber : [],
                    customerName : [],
                };
                list.forEach((item) => {
                    params['customerNumber'].push(item.customerNumber)
                    params['customerName'].push(item.customerName)
                })
                let formData = {
                    ...this.form,
                    customerNumber : params.customerNumber.join(','),
                    customerName : params.customerName.join(','),
                };
                const that = this;
                postAction(this.url.bind, formData).then((res) => {
                    if (res.success) {
                        that.$message.success(res.message)
                        that.selectCustomerList = [];
                        this.$emit('ok')
                        this.close();
                    } else {
                        that.$message.warning(res.message)
                    }
                }).finally(() => {
                })
            },
            onSelectChange(selectedRowKeys, selectedRows) {
                this.selectedRowKeys = selectedRowKeys;
                this.selectionRows = selectedRows;

                let data = [...this.selectCustomerList,...selectedRows];
                let list = [];
                data.forEach((item,index) =>{
                    let info = list.find((val) => val.customerNumber == item.customerNumber);
                    if(!info){
                        list.push(item);
                    }
                })
                this.selectCustomerList = list;
            },
            deleteData(info){
                this.selectCustomerList.forEach((item,index) =>{
                    if(item.customerNumber === item.customerNumber){
                        this.selectCustomerList.splice(index,1);
                    }
                })
            },
            handleCancel() {
                this.$emit('close')
                this.close();
            },
            close(){
                this.selectedRowKeys = [];
                this.selectionRows = [];
                this.selectCustomerList = [];
                this.visible = false;
                this.form.lineId = '';
            },
        }
    }
</script>

<style lang="less" scoped>
    .ant-card-body .table-operator {
        margin-bottom: 18px;
    }

    .ant-table-tbody .ant-table-row td {
        padding-top: 15px;
        padding-bottom: 15px;
    }

    .anty-row-operator button {
        margin: 0 5px
    }

    .ant-modal-cust-warp {
        height: 100%
    }

    .ant-modal-cust-warp .ant-modal-body {
        height: calc(100% - 110px) !important;
        overflow-y: auto
    }

    .ant-modal-cust-warp .ant-modal-content {
        height: 90% !important;
        overflow-y: hidden
    }
</style>
